﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GMS.BasicControls
{
    public partial class GroupList : BackGroundControl
    {
        public GroupList()
        {
            InitializeComponent();
        }
        private string _connectionString;
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }
        DataTable source = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter();

        private void GroupList_Load(object sender, EventArgs e)
        {
            source.RowChanged += new DataRowChangeEventHandler(source_RowChanged);
            source.TableNewRow += new DataTableNewRowEventHandler(source_TableNewRow);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = source;

            adapter.SelectCommand = new SqlCommand("DSelectGroups", new SqlConnection(ConnectionString));
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            adapter.Fill(source);

            adapter.InsertCommand = new SqlCommand("DInsertGroup", new SqlConnection(ConnectionString));
            adapter.InsertCommand.CommandType = CommandType.StoredProcedure;
            SqlParameter insertParameter = null;
            insertParameter = adapter.InsertCommand.Parameters.Add("@CaptionGroup", SqlDbType.VarChar);
            insertParameter.SourceColumn = "CaptionGroup";
            insertParameter.SourceVersion = DataRowVersion.Current;
            insertParameter = adapter.InsertCommand.Parameters.Add("@DescribeGroup", SqlDbType.VarChar);
            insertParameter.SourceColumn = "DescribeGroup";
            insertParameter.SourceVersion = DataRowVersion.Current;

            adapter.UpdateCommand = new SqlCommand("DUpdateGroup", new SqlConnection(ConnectionString));
            adapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
            SqlParameter updateParameter = null;
            updateParameter = adapter.UpdateCommand.Parameters.Add("@IdGroup", SqlDbType.Int);
            updateParameter.SourceColumn = "IdGroup";
            updateParameter.SourceVersion = DataRowVersion.Current;
            updateParameter = adapter.UpdateCommand.Parameters.Add("@CaptionGroup", SqlDbType.VarChar);
            updateParameter.SourceColumn = "CaptionGroup";
            updateParameter.SourceVersion = DataRowVersion.Current;
            updateParameter = adapter.UpdateCommand.Parameters.Add("@DescribeGroup", SqlDbType.VarChar);
            updateParameter.SourceColumn = "DescribeGroup";
            updateParameter.SourceVersion = DataRowVersion.Current;

        }

        void source_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            adapter.Update(source);
        }

        void source_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Change)
                adapter.Update(source);
        }

        private void applylinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Parent.Controls.Remove(this);
        }
    }
}
